<!DOCTYPE html>
<html>
<head>
    {include file="_Fragment/meta" /}
    <link rel="stylesheet" href="/static/pear/component/pear/css/pear.css" />
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form">
            <div class="layui-form-item">

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="课件名称" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="type">
                            <option value="">课件类型</option>
                            <option value="1">图文</option>
                            <option value="2">文档</option>
                            <option value="5">外链</option>
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="cate_id">
                            <option value="">课件分类</option>
                            {volist name="cate" id="vo"}
                            <option value="{$vo.id}">{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <select name="label_id">
                            <option value="">课件标签</option>
                            {volist name="label" id="vo"}
                            <option value="{$vo.id}">{$vo.title}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item layui-inline">
                    <div class="layui-input-inline">
                        <input type="text" name="time" id="date" placeholder="创建时间" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="user-table" lay-filter="user-table"></table>
    </div>
</div>

<script type="text/html" id="user-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="batchAdd">
        <i class="layui-icon layui-icon-add-1"></i>
        选择
    </button>
</script>

<script type="text/html" id="table-type">
    {{#if (d.type == '1') { }}
    <span>图文</span>
    {{# }else if(d.type == '2'){ }}
    <span>文档</span>
    {{# }else if(d.type == '5'){ }}
    <span>外链</span>
    {{# } }}
</script>
<script type="text/html" id="table-open">
    {{#if (d.open == '1') { }}
    <span>公开</span>
    {{# }else if(d.open == '2'){ }}
    <span>非公开</span>
    {{# } }}
</script>

<script type="text/html" id="create-time">
    {{layui.util.toDateString(d.add_time*1000, 'yyyy-MM-dd HH:mm:ss')}}
</script>

<script type="text/html" id="table-duration">
    {{d.duration/60}}
</script>
{include file="_Fragment/javascript" /}
<script>
    layui.use(['table', 'form', 'jquery', 'common', 'laydate'], function() {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let common = layui.common;
        let laydate = layui.laydate;

        let live_id = "{$live_id}";
        let unit_id = "{$unit_id}";
        laydate.render({
            elem: '#date'
            ,range: true //或 range: '~' 来自定义分割字符
            ,trigger: 'click'
            ,type: 'date'
        });

        let cols = [
            [{
                type: 'checkbox',
                fixed: 'left',
            },
                {
                    title: 'ID',
                    field: 'id',
                    align: 'center',
                    fixed: 'left',
                    width:'10%'
                },
                {
                    title: '课件名称(CN)',
                    field: 'title',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '课件名称(EN)',
                    field: 'e_title',
                    align: 'center',
                    fixed: 'left',
                    width:'20%'
                },
                {
                    title: '课件分类',
                    field: 'cate_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课件标签',
                    field: 'label_name',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '课件时长(分钟)',
                    field: 'duration',
                    align: 'center',
                    templet: '#table-duration',
                    width:'10%'
                },
                {
                    title: '课件类型',
                    field: 'type',
                    align: 'center',
                    templet:'#table-type',
                    width:'10%'
                },
                {
                    title: '是否公开',
                    field: 'open',
                    templet:'#table-open',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '创建人',
                    field: 'account',
                    align: 'center',
                    width:'10%'
                },
                {
                    title: '创建时间',
                    field: 'add_time',
                    align: 'center',
                    templet: '#create-time',
                    width:'20%'
                }
            ]
        ]

        table.render({
            elem: '#user-table',
            url: '/Live/getCoursewareList',
            page: {layout:['count', 'prev', 'page', 'next', 'skip']},
            limit: 5,
            cols: cols,
            //skin: 'line',
            toolbar: '#user-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter']
        });

        form.on('submit(user-query)', function(data) {
            table.reload('user-table', {
                where: data.field
                ,page: {
                    curr: 1, //重新从第 1 页开始
                    layout:['count', 'prev', 'page', 'next', 'skip']
                }
                ,limit: 5,
            })
            return false;
        });


        table.on('toolbar(user-table)', function(obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            } else if (obj.event === 'batchAdd') {
                window.batchAdd(obj);
            }
        });

        window.batchAdd = function(obj) {

            var checkIds = common.checkField(obj,'id');
            console.log(checkIds)
            if (checkIds === "") {
                layer.msg("未选中数据", {
                    icon: 3,
                    time: 1000
                });
                return false;
            }

            layer.confirm('确定要添加选中的课件吗?', {
                icon: 3,
                title: '提示'
            }, function(index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/Live/saveCourseware",
                    data:{id:checkIds, live_id:live_id, unit_id:unit_id},
                    dataType: 'json',
                    type: 'post',
                    success: function(result) {
                        layer.close(loading);
                        if (result.status == 0) {
                            layer.msg(result.msg, {
                                icon: 1,
                                time: 1000
                            }, function() {
                                parent.layer.close(parent.layer.getFrameIndex(window
                                    .name)); //关闭当前页
                                parent.location.reload();
                            });
                        } else {
                            layer.msg(result.msg, {
                                icon: 2,
                                time: 1000
                            });
                        }
                    }
                })
            });
        }

        window.refresh = function(param) {
            table.reload('user-table');
        }
    })
</script>
</body>
</html>
